// 1. 创建mixin.js并导出混入对象
export default {
	data() {
		return {
			str: 'mixin',
			useMockData: false,
			showMockDataSwitch: true // 开发环境显示
		}
	},
	mounted() {

		// 设置一个延迟，如果数据加载过慢，先显示模拟数据
		setTimeout(() => {
			if (this.tableData.length === 0) {
				this.loadMockData()
			}
		}, 1000)
	},

	methods: {
		// 设置图表数据
		setChartData(data) {
			if (this.activeChart === 'trend') {
				this.setTrendChartData(data)
			} else if (this.activeChart === 'product') {
				this.setProductChartData(data)
			} else if (this.activeChart === 'department') {
				this.setDepartmentChartData(data)
			}

			// 如果有真实图表库，使用真实图表
			if (typeof uCharts !== 'undefined' || typeof echarts !== 'undefined') {
				this.updateChart(data)
			} else {
				// 否则使用虚拟图表
				this.initVirtualChart(data)
			}
		},
		// 加载模拟数据
		loadMockData() {
			console.log('正在加载模拟数据...')
			this.generateMockData()

			// 显示提示
			uni.showToast({
				title: '使用模拟数据进行演示',
				icon: 'none',
				duration: 2000
			})
		},
		// 生成模拟数据（用于演示）
		generateMockData() {
			// 模拟统计概览数据
			this.statistics = {
				totalDeals: Math.floor(Math.random() * 100) + 50,
				totalAmount: (Math.random() * 500 + 100).toFixed(2),
				totalServiceFee: (Math.random() * 50 + 10).toFixed(2),
				avgAmount: (Math.random() * 10 + 3).toFixed(2)
			}

			// 模拟用户和部门数据
			this.userOptions = [{
					id: 'user1',
					name: '张三'
				},
				{
					id: 'user2',
					name: '李四'
				},
				{
					id: 'user3',
					name: '王五'
				},
				{
					id: 'user4',
					name: '赵六'
				}
			]

			this.deptOptions = [{
					id: 'dept1',
					name: '信贷部'
				},
				{
					id: 'dept2',
					name: '抵押部'
				},
				{
					id: 'dept3',
					name: '现金业务部'
				},
				{
					id: 'dept4',
					name: '企业融资部'
				}
			]

			// 生成模拟表格数据
			this.generateMockTableData()

			// 根据当前图表类型生成模拟图表数据
			this.generateMockChartData()
		},

		// 生成模拟表格数据
		generateMockTableData() {
			const products = ['信用贷款', '抵押贷款', '企业融资', '现金贷', '消费分期']
			const dealTypes = ['新客户成交', '续贷', '增贷', '转介绍']
			const departments = ['信贷部', '抵押部', '现金业务部', '企业融资部']
			const persons = ['张三', '李四', '王五', '赵六', '钱七']
			const customerNames = ['王先生', '李女士', '张总', '赵老板', '陈经理', '刘女士', '杨先生', '黄小姐']

			const mockData = []

			for (let i = 1; i <= 15; i++) {
				const date = new Date()
				date.setDate(date.getDate() - Math.floor(Math.random() * 30))

				const amount = (Math.random() * 100 + 10).toFixed(2)
				const serviceFee = (amount * 0.05).toFixed(2)

				mockData.push({
					id: `deal_${i}`,
					dealTime: date.toISOString(),
					customerName: customerNames[Math.floor(Math.random() * customerNames.length)],
					dealDepartment: departments[Math.floor(Math.random() * departments.length)],
					dealPerson: persons[Math.floor(Math.random() * persons.length)],
					product: products[Math.floor(Math.random() * products.length)],
					dealType: dealTypes[Math.floor(Math.random() * dealTypes.length)],
					amount: amount,
					serviceFee: serviceFee
				})
			}

			this.tableData = mockData
			this.total = 15
		},

		// 生成模拟图表数据
		generateMockChartData() {
			const chartType = this.activeChart

			switch (chartType) {
				case 'trend':
					this.setTrendChartData(this.generateTrendData())
					break
				case 'product':
					this.setProductChartData(this.generateProductData())
					break
				case 'department':
					this.setDepartmentChartData(this.generateDepartmentData())
					break
			}
		},

		// 生成趋势图数据
		generateTrendData() {
			const timeLabels = []
			const now = new Date()

			// 根据时间类型生成标签
			switch (this.filterForm.timeType) {
				case 'day':
					for (let i = 6; i >= 0; i--) {
						const date = new Date(now)
						date.setDate(date.getDate() - i)
						timeLabels.push(`${date.getMonth() + 1}/${date.getDate()}`)
					}
					break
				case 'week':
					for (let i = 4; i >= 0; i--) {
						timeLabels.push(`第${this.getWeekOfYear(now) - i}周`)
					}
					break
				case 'month':
					for (let i = 5; i >= 0; i--) {
						const date = new Date(now)
						date.setMonth(date.getMonth() - i)
						timeLabels.push(`${date.getFullYear()}-${date.getMonth() + 1}`)
					}
					break
				default: // year
					for (let i = 3; i >= 0; i--) {
						const year = now.getFullYear() - i
						timeLabels.push(`${year}年`)
					}
			}

			const trendData = timeLabels.map((label, index) => {
				const baseValue = 20 + index * 10
				return {
					date: label,
					dealCount: Math.floor(Math.random() * 20 + 5),
					totalAmount: (baseValue + Math.random() * 30).toFixed(2),
					totalServiceFee: ((baseValue + Math.random() * 30) * 0.05).toFixed(2)
				}
			})

			return trendData
		},

		// 生成产品分布数据
		generateProductData() {
			const products = [
				'信用贷款', '抵押贷款', '企业融资', '现金贷',
				'消费分期', '经营贷', '房贷', '车贷'
			]

			const productData = products.map(product => ({
				product: product,
				count: Math.floor(Math.random() * 50 + 10)
			}))

			// 按数量排序
			return productData.sort((a, b) => b.count - a.count).slice(0, 8)
		},

		// 生成部门分布数据
		generateDepartmentData() {
			const departments = [
				'信贷部', '抵押部', '现金业务部',
				'企业融资部', '个人金融部', '小微贷部'
			]

			const deptData = departments.map(dept => ({
				department: dept,
				amount: (Math.random() * 200 + 50).toFixed(2)
			}))

			// 按金额排序
			return deptData.sort((a, b) => b.amount - a.amount).slice(0, 6)
		},

		// 获取当前周数
		getWeekOfYear(date) {
			const firstDay = new Date(date.getFullYear(), 0, 1)
			const pastDays = (date - firstDay) / 86400000
			return Math.ceil((pastDays + firstDay.getDay() + 1) / 7)
		}
	},
}